/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.app.dto.UserLabelDto;
import com.yami.shop.bean.app.param.UserLabelParam;
import com.yami.shop.bean.model.UserLabel;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.UserLabelMapper;
import com.yami.shop.service.UserLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2018/10/26.
 */
@RestController
@RequestMapping("/admin/userLabel")
@Api(tags = "用户标签接口")
public class UserLabelController {

    @Autowired
    private UserLabelService userLabelService;
    @Autowired
    private MapperFacade mapperFacade;


    @GetMapping
    @ApiOperation(value = "分页获取标签", notes = "分页获取标签")
    @PreAuthorize("@pms.hasPermission('admin:userLabel:page')")
    public ResponseEntity<IPage<UserLabel>> list(PageParam<UserLabel> page, UserLabelParam userLabelParam) {
        Page<UserLabel> userLabelPage = userLabelService.page(page, Wrappers.lambdaQuery(new UserLabel())
                .eq(userLabelParam.getStatus() != null, UserLabel::getStatus, userLabelParam.getStatus())
                .orderByDesc(UserLabel::getStatus));
        return ResponseEntity.ok(userLabelPage);
    }

    @PutMapping
    @ApiOperation(value = "创建标签", notes = "创建标签")
    @PreAuthorize("@pms.hasPermission('admin:userLabel:save')")
    public ResponseEntity<Void> add(@RequestBody UserLabel userLabel) {
        userLabel.setId(IdUtil.simpleUUID());
        userLabel.setCreateTime(new Date());
        userLabel.setStatus(2);
        userLabelService.save(userLabel);
        return ResponseEntity.ok(null);
    }

    @PostMapping
    @ApiOperation(value = "更新审核状态", notes = "更新审核状态,只要id和status")
    @PreAuthorize("@pms.hasPermission('admin:userLabel:update')")
    public ResponseEntity<Void> update(@RequestBody UserLabel userLabel) {
        Assert.notNull(userLabel.getStatus(), "UserLabel的状态不能为空");
        UserLabel userLabel1 = new UserLabel();
        userLabel1.setId(userLabel.getId());
        userLabel1.setStatus(userLabel.getStatus());
        boolean b = userLabelService.updateById(userLabel1);
        return b ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

}
